<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="" prop="sectionId">
              <el-input v-model="queryParams.sectionId" placeholder="请输入" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="" prop="imgUrl">
              <el-input v-model="queryParams.imgUrl" placeholder="请输入" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="icon 图标地址" prop="iconUrl">
              <el-input v-model="queryParams.iconUrl" placeholder="请输入icon 图标地址" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="" prop="viewHeight">
              <el-input v-model="queryParams.viewHeight" placeholder="请输入" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="" prop="viewWidth">
              <el-input v-model="queryParams.viewWidth" placeholder="请输入" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="链接的id" prop="linkId">
              <el-input v-model="queryParams.linkId" placeholder="请输入链接的id" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="链接地址" prop="linkUrl">
              <el-input v-model="queryParams.linkUrl" placeholder="请输入链接地址" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="是否是切换菜单" prop="isSwitchTab">
              <el-input v-model="queryParams.isSwitchTab" placeholder="请输入是否是切换菜单" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="" prop="sort">
              <el-input v-model="queryParams.sort" placeholder="请输入" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="" prop="state">
              <el-input v-model="queryParams.state" placeholder="请输入" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="包邮、拼团、秒杀的icon图片  " prop="maskImg">
              <el-input v-model="queryParams.maskImg" placeholder="请输入包邮、拼团、秒杀的icon图片  " clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="lable文本  以逗号 , 分隔" prop="labelText">
              <el-input v-model="queryParams.labelText" placeholder="请输入lable文本  以逗号 , 分隔" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="" prop="btnText">
              <el-input v-model="queryParams.btnText" placeholder="请输入" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="" prop="btnTextCss">
              <el-input v-model="queryParams.btnTextCss" placeholder="请输入" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="Plus" @click="handleAdd" v-hasPermi="['shopping:menuSectionLinks:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['shopping:menuSectionLinks:edit']">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['shopping:menuSectionLinks:remove']">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" icon="Download" @click="handleExport" v-hasPermi="['shopping:menuSectionLinks:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="menuSectionLinksList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="" align="center" prop="sectionId" />
        <el-table-column label="" align="center" prop="imgUrl" />
        <el-table-column label="icon 图标地址" align="center" prop="iconUrl" />
        <el-table-column label="" align="center" prop="viewHeight" />
        <el-table-column label="" align="center" prop="viewWidth" />
        <el-table-column label="链接的类型   product - 单商品 - page 商品页" align="center" prop="linkType" />
        <el-table-column label="链接的id" align="center" prop="linkId" />
        <el-table-column label="链接地址" align="center" prop="linkUrl" />
        <el-table-column label="是否是切换菜单" align="center" prop="isSwitchTab" />
        <el-table-column label="遮罩的类型  0 - 正常无遮罩  1 - 已抢光" align="center" prop="maskType" />
        <el-table-column label="" align="center" prop="sort" />
        <el-table-column label="" align="center" prop="state" />
        <el-table-column label="包邮、拼团、秒杀的icon图片  " align="center" prop="maskImg" />
        <el-table-column label="lable文本  以逗号 , 分隔" align="center" prop="labelText" />
        <el-table-column label="" align="center" prop="btnText" />
        <el-table-column label="" align="center" prop="btnTextCss" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['shopping:menuSectionLinks:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['shopping:menuSectionLinks:remove']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <!-- 添加或修改每个内容行里的数据对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
      <el-form ref="menuSectionLinksFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="" prop="sectionId">
          <el-input v-model="form.sectionId" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="imgUrl">
          <el-input v-model="form.imgUrl" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="icon 图标地址" prop="iconUrl">
          <el-input v-model="form.iconUrl" placeholder="请输入icon 图标地址" />
        </el-form-item>
        <el-form-item label="" prop="viewHeight">
          <el-input v-model="form.viewHeight" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="viewWidth">
          <el-input v-model="form.viewWidth" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="链接的id" prop="linkId">
          <el-input v-model="form.linkId" placeholder="请输入链接的id" />
        </el-form-item>
        <el-form-item label="链接地址" prop="linkUrl">
          <el-input v-model="form.linkUrl" placeholder="请输入链接地址" />
        </el-form-item>
        <el-form-item label="是否是切换菜单" prop="isSwitchTab">
          <el-input v-model="form.isSwitchTab" placeholder="请输入是否是切换菜单" />
        </el-form-item>
        <el-form-item label="" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="state">
          <el-input v-model="form.state" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="包邮、拼团、秒杀的icon图片  " prop="maskImg">
          <el-input v-model="form.maskImg" placeholder="请输入包邮、拼团、秒杀的icon图片  " />
        </el-form-item>
        <el-form-item label="lable文本  以逗号 , 分隔" prop="labelText">
          <el-input v-model="form.labelText" placeholder="请输入lable文本  以逗号 , 分隔" />
        </el-form-item>
        <el-form-item label="" prop="btnText">
          <el-input v-model="form.btnText" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="btnTextCss">
          <el-input v-model="form.btnTextCss" placeholder="请输入" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="MenuSectionLinks" lang="ts">
import { listMenuSectionLinks, getMenuSectionLinks, delMenuSectionLinks, addMenuSectionLinks, updateMenuSectionLinks } from '@/api/shopping/titlemenu/section/links';
import { MenuSectionLinksVO, MenuSectionLinksQuery, MenuSectionLinksForm } from '@/api/shopping/titlemenu/section/links/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const menuSectionLinksList = ref<MenuSectionLinksVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const menuSectionLinksFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: MenuSectionLinksForm = {
  id: undefined,
  sectionId: undefined,
  imgUrl: undefined,
  iconUrl: undefined,
  viewHeight: undefined,
  viewWidth: undefined,
  linkType: undefined,
  linkId: undefined,
  linkUrl: undefined,
  isSwitchTab: undefined,
  maskType: undefined,
  sort: undefined,
  state: undefined,
  maskImg: undefined,
  labelText: undefined,
  btnText: undefined,
  btnTextCss: undefined
}
const data = reactive<PageData<MenuSectionLinksForm, MenuSectionLinksQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    sectionId: undefined,
    imgUrl: undefined,
    iconUrl: undefined,
    viewHeight: undefined,
    viewWidth: undefined,
    linkType: undefined,
    linkId: undefined,
    linkUrl: undefined,
    isSwitchTab: undefined,
    maskType: undefined,
    sort: undefined,
    state: undefined,
    maskImg: undefined,
    labelText: undefined,
    btnText: undefined,
    btnTextCss: undefined,
    params: {
    }
  },
  rules: {
    id: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
    sectionId: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
    imgUrl: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
    iconUrl: [
      { required: true, message: "icon 图标地址不能为空", trigger: "blur" }
    ],
    viewHeight: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
    viewWidth: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
    linkType: [
      { required: true, message: "链接的类型   product - 单商品 - page 商品页不能为空", trigger: "change" }
    ],
    linkId: [
      { required: true, message: "链接的id不能为空", trigger: "blur" }
    ],
    linkUrl: [
      { required: true, message: "链接地址不能为空", trigger: "blur" }
    ],
    isSwitchTab: [
      { required: true, message: "是否是切换菜单不能为空", trigger: "blur" }
    ],
    maskType: [
      { required: true, message: "遮罩的类型  0 - 正常无遮罩  1 - 已抢光不能为空", trigger: "change" }
    ],
    sort: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
    state: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
    maskImg: [
      { required: true, message: "包邮、拼团、秒杀的icon图片  不能为空", trigger: "blur" }
    ],
    labelText: [
      { required: true, message: "lable文本  以逗号 , 分隔不能为空", trigger: "blur" }
    ],
    btnText: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
    btnTextCss: [
      { required: true, message: "不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询每个内容行里的数据列表 */
const getList = async () => {
  loading.value = true;
  const res = await listMenuSectionLinks(queryParams.value);
  menuSectionLinksList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  menuSectionLinksFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: MenuSectionLinksVO[]) => {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = "添加每个内容行里的数据";
}

/** 修改按钮操作 */
const handleUpdate = async (row?: MenuSectionLinksVO) => {
  reset();
  const _id = row?.id || ids.value[0]
  const res = await getMenuSectionLinks(_id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = "修改每个内容行里的数据";
}

/** 提交按钮 */
const submitForm = () => {
  menuSectionLinksFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateMenuSectionLinks(form.value).finally(() =>  buttonLoading.value = false);
      } else {
        await addMenuSectionLinks(form.value).finally(() =>  buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess("操作成功");
      dialog.visible = false;
      await getList();
    }
  });
}

/** 删除按钮操作 */
const handleDelete = async (row?: MenuSectionLinksVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除每个内容行里的数据编号为"' + _ids + '"的数据项？').finally(() => loading.value = false);
  await delMenuSectionLinks(_ids);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('shopping/menuSectionLinks/export', {
    ...queryParams.value
  }, `menuSectionLinks_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList();
});
</script>
